// IFManager.cpp: implementation of the CIFManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "log4cplus_switch.h"
#include <string>
#include <list>
#include <map>
#include <common/Pool.h>
#include "NETLIB\TCPClient.h"
#include "cmpp_define.h"
#include "IF.h"
#include "CMPPClient.h"
#include "netlib/SocketEngine.h"
#include "IFManager.h"
#include "SMSItem.h"
#include "SMSDispatch.h"
#include "AccountManager.h"
#include "SMSApp.h"

LOG4CPLUS_CLASS_IMPLEMENT( CIFManager, _s_logger, "CIFManager" );

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIFManager::CIFManager()
{

}

CIFManager::~CIFManager()
{

}

BOOL CIFManager::SendOut(CAccount* pAccount, CSMSItem* pSMS)
{
	//find an active IF in this account, and send, if not found or fail return FALSE
	std::list<CIF*>::iterator it;
	for(it =m_IFList.begin(); it!=m_IFList.end(); it++)
	{
		//find an active IF in this account
		//return a dummy here
		CIF* pIF = *it;
		return pIF->Send(pSMS);  //really send out SMS here 
		break;
	}
	return TRUE;

}

BOOL CIFManager::AddInterface(CAccount* pAccount, int protocol, const char *destIP, unsigned short port)
{
	CCMPPClient* pCMPPClient = new CCMPPClient();
	pCMPPClient->Connect(destIP, port, 3000);
	CSMSApp::GetInstance()->m_Engine.Add(pCMPPClient);
	//set parent
	pCMPPClient->SetParent(pAccount);
	m_IFList.push_back((CIF*)pCMPPClient);
    return TRUE;
}

void CIFManager::PeriodDispatch()
{
	//connect manager
	std::list<CIF*>::iterator it;
	for(it = m_IFList.begin(); it!=m_IFList.end();it++)
	{
		CIF* p = *it;
		p->PeriodDispatch();
	}
}
